package wat.serwerapp.usermgmt.ldap;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStream;
import java.security.Policy.Parameters;
import java.util.Hashtable;
import java.util.Properties;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.naming.Context;
import javax.naming.NamingException;
import javax.naming.directory.DirContext;
import javax.naming.directory.InitialDirContext;


/**
 *
 * @author P.B. & L.G. & K.K.
 */
public class LdapConnector {

    public static final String SUFIX = ",dc=wat,dc=edu,dc=pl";
    private static Hashtable properties;

    static {
        properties = new Hashtable(11);
        properties.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.ldap.LdapCtxFactory");
        properties.put(Context.PROVIDER_URL, wat.serwerapp.main.Parameters.getInstance().getLDAPConnection());
    }

    private LdapConnector() {
    }

    public static DirContext getUserContext(String login, String password) throws NamingException {
        Hashtable prop = (Hashtable) properties.clone();
        prop.put(Context.SECURITY_PRINCIPAL, "cn=" + login + UserManager.User.USER_SUFIX);
        prop.put(Context.SECURITY_CREDENTIALS, password);
        DirContext ctx = new InitialDirContext(prop);
        return ctx;
    }
    
    public static DirContext getRolesContext() throws NamingException {
        Hashtable prop = (Hashtable) properties.clone();
        DirContext ctx = new InitialDirContext(prop);
        return ctx;
    }
}
